package test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class PermuteUnique2 {
	public List<List<Integer>> ret;
    public List<Integer> path;
    public boolean[] check;
    public void dfs(int[] nums,int pos){
        if(pos==nums.length){
            ret.add(new ArrayList(path));
            return ;
        }

        //pos来控制path的大小
        //for来枚举元素，只要不满足就一直来遍历，满足条件才带下一层
        for(int i=0;i<nums.length;i++){
            if(check[i]==true||((i!=0)&&(nums[i]==nums[i-1])&&check[i-1]==false))
                continue;
            path.add(nums[i]);
            check[i]=true;
            dfs(nums,pos+1);
            path.remove(path.size()-1);
            check[i]=false;
        }
        
    }
    public List<List<Integer>> permuteUnique(int[] nums) {
        ret=new ArrayList<>();
        path=new ArrayList();
        check=new boolean[nums.length];   
        Arrays.sort(nums);
        dfs(nums,0);
        return ret;
    }
}
